// pages/editperson/editperson.js
const md = require('../../utils/md5.js');
const app = getApp()
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['福建省', '厦门市', '思明区'],
    constellation: ['水瓶座', '双鱼座','白羊座','金牛座','双子座','巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座','摩羯座'],
    index:0,
    sexarr:['男','女'],
    sex:0,
    userinfo: wx.getStorageSync('userinfo'),
    showsex:'男',
    showxinzuo:'水瓶座',
    showarea:"福建省厦门市思明区"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var time = util.formatTime(new Date());
    time = time.split(' ')[0];
    time = time.split('-');
    that.setData({
      year: time[0],
      month: time[1],
      day: time[2],
      img2: wx.getStorageSync('userinfo').PhotoUrl,
      photourl: wx.getStorageSync('userinfo').PhotoUrl
    })
  },
  onShow:function(){
    var that =this;
    that.setData({
      userinfo: wx.getStorageSync('userinfo')
    })
    if (that.data.userinfo.Address){
      if (that.data.userinfo.Address.length > 0){
        that.setData({
          showarea: that.data.userinfo.Address
        })
      } 
    };
    if (that.data.userinfo.Sex) {
      if (that.data.userinfo.Sex.length > 0){
        that.setData({
          showsex: that.data.userinfo.Sex
        })
      }
    };
    // if (that.data.userinfo.Constellatory.length >0) {
    //   that.setData({
    //     showxinzuo: that.data.userinfo.Constellatory
    //   })
    // }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  choosemethod:function(){
    var that = this;
    wx.showActionSheet({
      itemList: ['使用微信头像', '上传自定义图片'],
      success: function (res) {
        console.log(res.tapIndex)
        // console.log(UserInfo('avatarUrl'));
        if (res.tapIndex==0){
          wx.getUserInfo({
            success: function (res) {
              let imgurl = JSON.parse(res.rawData).avatarUrl;
              that.setData({
                img2: imgurl,
                photourl: imgurl
              })

            }
          })
        }else{
          that.chooseimg();
        }
        
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },
  chooseimg(e) {
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);
        var apinameIsMeb = "/File/Upload";
        var timestamp = new Date().getTime();
        timestamp = parseInt(timestamp / 1000);
        var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: app.server.url + apinameIsMeb, //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file[]',
          method: 'POST',
          header: {
            'content-type': 'multipart/form-data'
          },
          formData: {
            "sign": md5IsMeb,
            "appid": app.server.appid,
            "tmsp": timestamp,
            "objID": 666,
            "file": that.data.year + "/" + that.data.month + "/" + that.data.day + "/" + new Date().getTime() + Math.ceil(Math.random() * 1000) + ".jpg"
          },
          success: function (res) {
            wx.hideLoading();
            var data = res.data;
            data = JSON.parse(data);
            console.log(data.data)
            if (data.data != '') {
              that.setData({
                img2: "http://eplus.zokoo.com.cn/upload/" + data.data,
                photourl: "http://eplus.zokoo.com.cn/upload/" + data.data
              })
            }
          }
        })
      }
    })
  },
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    var info = e.detail.value;
    // 获取时间戳
    if (info.name==''){
      wx.showModal({
        title: '',
        content: '昵称不能为空！',
      })
      return;
    }
    var apinameIsMeb = "/BO/BA_Member/Modify";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    var data={};
    data.MebID = (wx.getStorageSync('userinfo')).MebID;
    data.Sex = info.sex;
    data.NickName = info.name;
    data.Constellatory = info.constellation;
    data.Email =info.email;
    data.Address=info.address;
    data.PhotoUrl = this.data.photourl;
    data = JSON.stringify(data);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        data:data
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        // this.globalData.userInfo = JSON.parse(res.data);
        console.log(res);
        if(res.data.code==0){
          wx.switchTab({
            url: '/pages/mine/mine',
          })
        }
      }
    })
    return;
    wx.switchTab({
      url: '/pages/mine/mine',
    })
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value,
      address: e.detail.value[0]+e.detail.value[1]+e.detail.value[2],
      showarea: e.detail.value[0] + e.detail.value[1] + e.detail.value[2]
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value,
      showxinzuo: this.data.constellation[e.detail.value]
    })
  },
  bindPickerChange2: function (e) {
    this.setData({
      sex: e.detail.value,
      showsex: this.data.sexarr[e.detail.value]
    })
  },
})